package com.kcsj.dao;

import com.kcsj.domain.WarehouseOutRecord;
import com.kcsj.domain.ProductModel;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;


public class WarehouseOutRecordDao {
    private static WarehouseOutRecordDao warehouseOutRecordDao = new WarehouseOutRecordDao();
    public static WarehouseOutRecordDao getInstance(){return warehouseOutRecordDao;}

    private WarehouseOutRecordDao(){}

    public WarehouseOutRecord find(int id, Connection conn) throws SQLException {
        //根据ID查找入库记录的语句的SQL语句
        String findAll_sql = "SELECT * FROM warehouseoutrecord WHERE id=?";
        //用于返回的入库记录对象
        WarehouseOutRecord warehouseOutRecord = null;
        //预编译SQL语句
        PreparedStatement pstmt = conn.prepareStatement(findAll_sql);
        //设置查找的ID
        pstmt.setInt(1, id);
        //执行并获取结果集
        ResultSet rs = pstmt.executeQuery();
        try {
            while (rs.next()){
                //获取标签信息
                String tagInfomation = rs.getString("tagInformation");
                //获取入库时间
                LocalDateTime outboudTime = rs.getTimestamp("outboundTime").toLocalDateTime();
                //获取备注
                String remarks = rs.getString("remarks");
                //获取产品型号对象
                ProductModel productModel = ProductModelDao.getInstance().find(rs.getString("productModelNo"), conn);
                //构建对象并加入集合
                warehouseOutRecord = new WarehouseOutRecord(id,productModel,tagInfomation,outboudTime,remarks);
            }
        }finally {
            JdbcHelper.close(rs, pstmt);
        }

        return warehouseOutRecord;
    }

    //查找所有入库记录
    public Collection<WarehouseOutRecord> findAll(List<Condition> conditionList, Connection conn) throws SQLException {
        Collection<WarehouseOutRecord> warehouseOutRecords = new HashSet<>();
        String findAll_sql = "SELECT * FROM warehouseoutrecord";
        if (conditionList != null) {
            findAll_sql += Condition.toWhereClause(conditionList);
        }
        PreparedStatement pstmt = conn.prepareStatement(findAll_sql);
        ResultSet rs = pstmt.executeQuery();try {
            while (rs.next()){
                //获取ID
                int id = rs.getInt("id");
                //获取标签信息
                String tagInformation = rs.getString("tagInformation");
                //获取入库时间
                LocalDateTime outboudTime = rs.getTimestamp("outboundTime").toLocalDateTime();
                //获取备注
                String remarks = rs.getString("remarks");
                //获取产品型号对象
                ProductModel productModel = ProductModelDao.getInstance().find(rs.getString("productModelNo"), conn);
                //构建对象并加入集合
                warehouseOutRecords.add(new WarehouseOutRecord(id,productModel,tagInformation,outboudTime,remarks));
            }
        }finally {
            JdbcHelper.close(rs, pstmt);
        }
        return warehouseOutRecords;
    }

    //插入入库记录
    public int add(WarehouseOutRecord warehouseOutRecord, Connection conn)throws SQLException{
            String add_sql ="INSERT INTO warehouseoutrecord(productModelNo,taginformation,remarks) VALUES (?,?,?)";
            //创建预编译语句，设置返回主键
            PreparedStatement pstmt = conn.prepareStatement(add_sql, Statement.RETURN_GENERATED_KEYS);
            //设置参数
            pstmt.setString(1, warehouseOutRecord.getProductModel().getProductModelNo());
            pstmt.setString(2, warehouseOutRecord.getTagInformation());
            pstmt.setString(3, warehouseOutRecord.getRemarks());
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
            int id_generated = 0;
            try {
                //获取生成的主键
                while (rs.next()){
                    id_generated = rs.getInt(1);
                }
            }finally {
                JdbcHelper.close(rs, pstmt);
            }

            //返回主键，如果id为0则说明未正确插入
            return id_generated;


    }

    public boolean addAll(List<WarehouseOutRecord> warehouseOutRecords, Connection conn)throws SQLException{
        String add_sql ="INSERT INTO warehouseoutrecord(productModelNo,taginformation,remarks) VALUES (?,?,?)";
        //创建预编译语句，设置返回主键
        PreparedStatement pstmt = conn.prepareStatement(add_sql);
        //设置参数
        try {
            for (WarehouseOutRecord warehouseOutRecord : warehouseOutRecords) {
                pstmt.setString(1, warehouseOutRecord.getProductModel().getProductModelNo());
                pstmt.setString(2, warehouseOutRecord.getTagInformation());
                pstmt.setString(3, warehouseOutRecord.getRemarks());
                pstmt.addBatch();
                pstmt.executeBatch();
            }
        }catch (SQLException e) {
            return false;
        }finally {
            JdbcHelper.close(null, pstmt);
        }
        return true;
    }

}
